package com.xc.utils.redis;

import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.ShardedJedis;

/**
 *  redis sharded pool util
 * Created by xiongcan on 2018/1/22.
 */
@Slf4j
public class RedisShardedPoolUtils {

    //  redis set method
    public static String set(String key, String value){
        ShardedJedis jedis = null;
        String result = null;

        try {
            jedis = RedisShardedPool.getJedis();
            result = jedis.set(key,value);
        }catch (Exception e){
            log.error("redis set key: {} value: {} error" , key , value ,e);
            RedisShardedPool.returnBrokenResouce(jedis);
            return result;
        }
        RedisShardedPool.returnResouce(jedis);
        return result;
    }

    //  redis get method
    public static String get(String key){
        ShardedJedis jedis = null;
        String result = null;

        try {
            jedis = RedisShardedPool.getJedis();
            result = jedis.get(key);
        }catch (Exception e){
            log.error("redis get key: {} error" , key , e);
            RedisShardedPool.returnBrokenResouce(jedis);
            return result;
        }
        RedisShardedPool.returnResouce(jedis);
        return result;
    }

    //  redis setEx method  , exTime 单位为秒
    public static String setEx(String key, String value, int exTime){
        log.info("redis setEx key: {}   value: {}" , key , value );
        ShardedJedis jedis = null;
        String result = null;

        try {
            jedis = RedisShardedPool.getJedis();
            result = jedis.setex(key , exTime , value);
        }catch (Exception e){
            log.error("redis setEx key: {} value: {}   error..." , key , value , e);
            RedisShardedPool.returnBrokenResouce(jedis);
            return result;
        }
        RedisShardedPool.returnResouce(jedis);
        return result;
    }

    //  redis setEx method  ， exTime单位是秒
    public static Long expire(String key , int exTime){
        ShardedJedis jedis = null;
        Long result = null;

        try {
            jedis = RedisShardedPool.getJedis();
            result = jedis.expire(key , exTime);
        }catch (Exception e){
            log.error("redis expire key: {}  error " , key , e);
            RedisShardedPool.returnBrokenResouce(jedis);
            return result;
        }
        RedisShardedPool.returnResouce(jedis);
        return result;
    }


    //  redis get method
    public static Long del(String key){
        ShardedJedis jedis = null;
        Long result = null;

        try {
            jedis = RedisShardedPool.getJedis();
            result = jedis.del(key);
        }catch (Exception e){
            log.error("redis del key: {} error " , key , e);
            RedisShardedPool.returnBrokenResouce(jedis);
            return result;
        }
        RedisShardedPool.returnResouce(jedis);
        return result;
    }


    public static void main(String[] args) {
        ShardedJedis jedis = RedisShardedPool.getJedis();

        System.out.println(RedisShardedPoolUtils.get("key1"));
        System.out.println(RedisShardedPoolUtils.get("key2"));
        System.out.println(RedisShardedPoolUtils.get("key3"));

        System.out.println("redis shaded pool utils end ...");
    }


}
